<template>
  <div class="forgot-password-container">
    <div class="forgot-password-box">
      <div class="forgot-password-header">
        <div class="logo">
          <i class="el-icon-key"></i>
        </div>
        <h1 class="forgot-password-title">密码重置</h1>
        <p class="forgot-password-subtitle">找回账号 · 安全验证 · 重置密码</p>
      </div>

      <el-form
        :model="forgotForm"
        :rules="forgotRules"
        ref="forgotFormRef"
        class="forgot-password-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="forgotForm.username"
            placeholder="请输入用户名"
            size="large"
          >
            <template #prefix>
              <i class="el-icon-user"></i>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="newPassword">
          <el-input
            v-model="forgotForm.newPassword"
            placeholder="请输入新密码"
            type="password"
            size="large"
            show-password
          >
            <template #prefix>
              <i class="el-icon-lock"></i>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="confirmPassword">
          <el-input
            v-model="forgotForm.confirmPassword"
            placeholder="请确认新密码"
            type="password"
            size="large"
            show-password
          >
            <template #prefix>
              <i class="el-icon-lock"></i>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            class="forgot-password-btn"
            @click="handleResetPassword"
            :loading="loading"
          >
            {{ loading ? '处理中...' : '重置密码' }}
          </el-button>
        </el-form-item>
      </el-form>

      <div class="login-footer">
        <router-link to="/login" class="link-item">
          <i class="el-icon-back"></i>
          <span>返回登录</span>
        </router-link>
      </div>
    </div>

    <div class="copyright">
      ©2023 智慧零工平台 版权所有
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { resetPassword } from '../api/auth'

const router = useRouter()
const forgotFormRef = ref(null)
const loading = ref(false)

const forgotForm = reactive({
  username: '',
  newPassword: '',
  confirmPassword: ''
})

const validatePass2 = (rule, value, callback) => {
  if (value !== forgotForm.newPassword) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const forgotRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 4, max: 20, message: '用户名长度在4到20个字符之间', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 8, max: 20, message: '密码长度在8到20个字符之间', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    { validator: validatePass2, trigger: 'blur' }
  ]
}

const handleResetPassword = () => {
  forgotFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        await resetPassword({
          username: forgotForm.username,
          newPassword: forgotForm.newPassword
        })
        ElMessage.success('密码重置成功，请重新登录')
        router.push('/login')
      } catch (error) {
        ElMessage.error(error.message || '密码重置失败')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.forgot-password-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: url('image/Login.jpg') no-repeat center center / cover;
  background-color: rgba(0, 0, 0, 0.7);
  padding: 20px;
  position: relative;
}

.forgot-password-box {
  width: 400px;
  padding: 40px 30px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 20px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.6);
  text-align: center;
  animation: floatIn 1s ease-out forwards;
}

@keyframes floatIn {
  0% { opacity: 0; transform: translateY(40px) scale(0.95); }
  100% { opacity: 1; transform: translateY(0) scale(1); }
}

.forgot-password-header {
  margin-bottom: 30px;
}

.logo i {
  font-size: 48px;
  color: #5dade2;
  margin-bottom: 10px;
  animation: pulse 2s infinite alternate;
}

@keyframes pulse {
  0% { transform: scale(1); opacity: 1; }
  100% { transform: scale(1.1); opacity: 0.8; }
}

.forgot-password-title {
  font-size: 32px;
  font-weight: bold;
  background: linear-gradient(90deg, #6dd5fa, #2980b9);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.forgot-password-subtitle {
  font-size: 14px;
  color: #5c5959;
  margin-top: 8px;
}

.forgot-password-form {
  margin-top: 30px;
}

.el-input, .el-button {
  transition: all 0.3s ease;
}

.el-input input {
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: #fff;
  padding: 10px;
  font-size: 16px;
  border-radius: 8px;
}

.el-input input::placeholder {
  color: #bbb;
}

.el-input:hover input, .el-input:focus-within input {
  border-color: #409EFF;
  box-shadow: 0 0 10px rgba(64, 158, 255, 0.6);
}

.forgot-password-btn {
  width: 100%;
  height: 48px;
  font-size: 18px;
  margin-top: 20px;
  background: linear-gradient(90deg, #409EFF, #66b1ff);
  border: none;
  border-radius: 10px;
  color: white;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.forgot-password-btn::after {
  content: "";
  position: absolute;
  top: 0; left: 50%;
  width: 300%;
  height: 300%;
  background: radial-gradient(circle, rgba(255,255,255,0.3) 10%, transparent 10.01%);
  background-repeat: no-repeat;
  background-position: 50%;
  transform: translate(-50%, -50%) scale(0);
  transition: 0.5s ease;
}

.forgot-password-btn:active::after {
  transform: translate(-50%, -50%) scale(1);
  transition: 0s;
}

.forgot-password-btn:hover {
  background: linear-gradient(90deg, #66b1ff, #409EFF);
  box-shadow: 0 0 16px rgba(64, 158, 255, 0.7);
}

.login-footer {
  margin-top: 40px;
  font-size: 12px;
  color: #aaa;
}

.link-item {
  color: #aaa;
  text-decoration: none;
  font-size: 13px;
  transition: color 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

.link-item:hover {
  color: #409EFF;
}

.copyright {
  position: absolute;
  bottom: 20px;
  text-align: center;
  width: 100%;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.4);
}

/* 小屏幕优化 */
@media (max-width: 768px) {
  .forgot-password-box {
    width: 90%;
    padding: 30px 20px;
  }
}
</style>